import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Test5 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        Thread customer = new Thread(()->{
            while (true){
                try{
                    int value = blockingQueue.take();
                    System.out.println("消费元素"+value);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者");
        Thread producer = new Thread(()->{
            Random random = new Random();
            while (true){
                try{
                    int num = random.nextInt(1000);
                    System.out.println("生产元素"+num);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"生产者");
        producer.start();
        customer.join();
        producer.join();
    }
}
